package com.woniu.smallmedical.controller;

import com.woniu.smallmedical.entity.*;
import com.woniu.smallmedical.server.GraphInfoServer;
import com.woniu.smallmedical.server.PermissionServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import sun.reflect.generics.tree.Tree;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author author
 * @create 2021-10-08-14:04
 */
@CrossOrigin
@RestController
public class PermissionController {

    @Autowired
    PermissionServer permissionServer;

    @RequestMapping("/getRoleInfo")
    public ResponseResult getRole(){
        List<Role> role = permissionServer.getRole();

        return new ResponseResult(role,200,"success");
    }

    @RequestMapping("/getTreeMenu")
    public ResponseResult getTreeMenu(){
        List<TreeMenu> treeMenu = permissionServer.getTreeMenu();
        System.out.println(treeMenu);
        return new ResponseResult(treeMenu,200,"success");
    }
    @RequestMapping("/getPermission")
    public ResponseResult getPermission(){
        List<Permission> permission = permissionServer.getPermission();
        System.out.println(permission);
        return new ResponseResult(permission,200,"success");
    }



    @RequestMapping("/getMenuPermissionInfo/{rid}")
    public ResponseResult getMenuPermissionInfo(@PathVariable("rid") Integer rid){
        Role treeMenuByRid = permissionServer.getTreeMenuByRid(rid);
        System.out.println(treeMenuByRid.getRolePermission());
        /*按钮权限回显逻辑*/
       List<Permission> permissions=new ArrayList<>();
       permissions=treeMenuByRid.getRolePermission();
       int[] pid=new int[permissions.size()];
        int[] ppid=new int[permissions.size()];
       List<Integer> pids=new ArrayList<>();
        List<Integer> pidss=new ArrayList<>();
       for(int i=0;i<permissions.size();i++){
                ppid[i]=permissions.get(i).getPid();
            for(int j=0;j<permissions.size();j++)
            {
                if(permissions.get(i).getPid()==permissions.get(j).getPpid())
                {
                    pid[i]=(permissions.get(j).getPid());
                }
            }
       }
        pids=combineArray(pid, ppid);

        for (int i =0; i < pids.size(); i ++) {
            if(pids.get(i)!=0) {
                pidss.add(pids.get(i));
            }
        }


        /*菜单权限回显逻辑*/
        List<RoleMenu> roleMenu = treeMenuByRid.getRoleMenu();

        int[] pmids=new int[roleMenu.size()];
        int[] mids=new int[roleMenu.size()];
        List<Integer> midss=new ArrayList<>();
        List<Integer> midsss=new ArrayList<>();
        for(int i=0;i<roleMenu.size();i++)
        {   mids[i]=roleMenu.get(i).getMid();
            for(int j=0;j<roleMenu.size();j++)
            {
                if(roleMenu.get(i).getMid()==roleMenu.get(j).getPmid()){

                    pmids[i]=roleMenu.get(i).getMid();
                }
            }

        }
        midss=combineArray(mids, pmids);

        for (int i =0; i < midss.size(); i ++) {
            if(midss.get(i)!=0) {
                midsss.add(midss.get(i));
            }
        }
//        for(int i=0;i<mids.length;i++)
//        {
//            for(int j=0;j<pmids.length;j++){
//                if((mids[i]==pmids[j])){
//                    midss.add(mids[i]);
//                }
//
//            }
//        }

//        List<RoleMenu> roleMenu = treeMenuByRid.getRoleMenu();
//        Integer[] mids=new Integer[roleMenu.size()];
//        Integer[] midsChildren=new Integer[200];
//        for(int i=0;i<roleMenu.size();i++){
//            if(roleMenu.get(i).getChildren().size()==0) {
//                mids[i] = roleMenu.get(i).getTreemenu().getMid();
//            }
//            if(roleMenu.get(i).getChildren().size()>0)
//            {
//                for(int j=0;j<roleMenu.get(i).getChildren().size();j++)
//                {
//                    midsChildren[j]=roleMenu.get(i).getChildren().get(j).getTreemenu().getMid();
//                }
//            }
//        }
//        Integer[] sumMids=new Integer[200+roleMenu.size()];
//
//        System.arraycopy(mids, 0, sumMids, 0, mids.length);
//        System.arraycopy(midsChildren, 0, sumMids, mids.length, midsChildren.length);
//        List<Integer> midss=new ArrayList<>();
//        for(Integer mid:sumMids){
//            if(mid!=null) {
//                midss.add(mid);
//            }
//        }
        ArrayListPermission arrayListPermission=new ArrayListPermission();
        arrayListPermission.setMids(midsss);
        arrayListPermission.setPids(pidss);
        return new ResponseResult(arrayListPermission,200,"success");
    }


    @GetMapping("/updateMenuPermission/{rid}/{mid}/{pid}")
    public ResponseResult updateMenuPermission(@PathVariable("rid") Integer rid,@PathVariable("mid")Integer[] mid,@PathVariable("pid") Integer[] pid){


//        TreeMenu treeMenuByMid = permissionServer.getTreeMenuByMid(mid);
//        if(treeMenuByMid.getMpid()!=0){
//            TreeMenu treeMenuByMpid = permissionServer.getTreeMenuByMpid(treeMenuByMid.getMpid());
//        }
//        RoleMenu roleMenu=new RoleMenu();
////        roleMenu.setMid(mid);
//        roleMenu.setPmid(treeMenuByMid.getMpid());
//        roleMenu.setRid(rid);
            permissionServer.updateMenuPermission(rid,mid,pid);
        return new ResponseResult(null,200,"授权成功");
    }





















    public static List combineArray(int[] arr, int[] arr1) {
        List list = Arrays.stream(arr).boxed().collect(Collectors.toList());

        List list1 = Arrays.stream(arr1).boxed().collect(Collectors.toList());

        List listAll = new ArrayList<>();

        listAll.addAll(list);

        listAll.addAll(list1);

        list1.retainAll(list);

        listAll.removeAll(list1);

        return listAll;

    }
}
